﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using RestFullChatAJAX.Models;

namespace RestFullChatAJAX.Controllers
{
    public class SendMessageController : ApiController
    {
        // POST api/Message/username?userreceive&message
        public HttpStatusCode PostMessage(String id, [FromBody] MessageSend info)
        {
            int index = -1;
            if (info.UserReceive.Contains('-'))
                index = IndexManager.FindStoreIndex(info.UserReceive.Split('-')[0].Trim());
            else index = IndexManager.FindStoreIndex(info.UserReceive.Trim());
            /*
             * message form: "userSend,content"
             */
            if (index == -1)
                return HttpStatusCode.BadRequest;
            if (String.IsNullOrEmpty(info.Message))
                return HttpStatusCode.OK;
            String message = id.ToString() + ',' + info.UserReceive + ',' + info.Message;
            MessageController.Add(index,message);
            return HttpStatusCode.OK;
        }
    }
}
